<template>
  <el-dialog
    :title="!dataForm.buyId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="活动名称" prop="buyName">
      <el-input v-model="dataForm.buyName" placeholder="活动名称"></el-input>
    </el-form-item>
    <el-form-item label="生效时间" prop="buyStarttime">
      <el-input v-model="dataForm.buyStarttime" placeholder="生效时间"></el-input>
    </el-form-item>
    <el-form-item label="过期时间" prop="buyExprietime">
      <el-input v-model="dataForm.buyExprietime" placeholder="过期时间"></el-input>
    </el-form-item>
    <el-form-item label="拼团库存" prop="buyCount">
      <el-input v-model="dataForm.buyCount" placeholder="拼团库存"></el-input>
    </el-form-item>
    <el-form-item label="拼团价格" prop="buyPrice">
      <el-input v-model="dataForm.buyPrice" placeholder="拼团价格"></el-input>
    </el-form-item>
    <el-form-item label="成团人数" prop="buyNum">
      <el-input v-model="dataForm.buyNum" placeholder="成团人数"></el-input>
    </el-form-item>
    <el-form-item label="成团时效" prop="buyAging">
      <el-input v-model="dataForm.buyAging" placeholder="成团时效"></el-input>
    </el-form-item>
    <el-form-item label="限购数量" prop="buyAmount">
      <el-input v-model="dataForm.buyAmount" placeholder="限购数量"></el-input>
    </el-form-item>
    <el-form-item label="运费" prop="buyFreight">
      <el-input v-model="dataForm.buyFreight" placeholder="运费"></el-input>
    </el-form-item>
    <el-form-item label="分享描述" prop="buyMs">
      <el-input v-model="dataForm.buyMs" placeholder="分享描述"></el-input>
    </el-form-item>
    <el-form-item label="活动封面" prop="buyUrl">
      <el-input v-model="dataForm.buyUrl" placeholder="活动封面"></el-input>
    </el-form-item>
    <el-form-item label="商品ID" prop="shopId">
      <el-input v-model="dataForm.shopId" placeholder="商品ID"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="shopUrl">
      <el-input v-model="dataForm.shopUrl" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="shopName">
      <el-input v-model="dataForm.shopName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品单价" prop="shopPrice">
      <el-input v-model="dataForm.shopPrice" placeholder="商品单价"></el-input>
    </el-form-item>
    <el-form-item label="商品库存" prop="shopRepertory">
      <el-input v-model="dataForm.shopRepertory" placeholder="商品库存"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          buyId: 0,
          buyName: '',
          buyStarttime: '',
          buyExprietime: '',
          buyCount: '',
          buyPrice: '',
          buyNum: '',
          buyAging: '',
          buyAmount: '',
          buyFreight: '',
          buyMs: '',
          buyUrl: '',
          shopId: '',
          shopUrl: '',
          shopName: '',
          shopPrice: '',
          shopRepertory: ''
        },
        dataRule: {
          buyName: [
            { required: true, message: '活动名称不能为空', trigger: 'blur' }
          ],
          buyStarttime: [
            { required: true, message: '生效时间不能为空', trigger: 'blur' }
          ],
          buyExprietime: [
            { required: true, message: '过期时间不能为空', trigger: 'blur' }
          ],
          buyCount: [
            { required: true, message: '拼团库存不能为空', trigger: 'blur' }
          ],
          buyPrice: [
            { required: true, message: '拼团价格不能为空', trigger: 'blur' }
          ],
          buyNum: [
            { required: true, message: '成团人数不能为空', trigger: 'blur' }
          ],
          buyAging: [
            { required: true, message: '成团时效不能为空', trigger: 'blur' }
          ],
          buyAmount: [
            { required: true, message: '限购数量不能为空', trigger: 'blur' }
          ],
          buyFreight: [
            { required: true, message: '运费不能为空', trigger: 'blur' }
          ],
          buyMs: [
            { required: true, message: '分享描述不能为空', trigger: 'blur' }
          ],
          buyUrl: [
            { required: true, message: '活动封面不能为空', trigger: 'blur' }
          ],
          shopId: [
            { required: true, message: '商品ID不能为空', trigger: 'blur' }
          ],
          shopUrl: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          shopName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          shopPrice: [
            { required: true, message: '商品单价不能为空', trigger: 'blur' }
          ],
          shopRepertory: [
            { required: true, message: '商品库存不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.buyId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.buyId) {
            this.$http({
              url: this.$http.adornUrl(`/commerce/buy/info/${this.dataForm.buyId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.buyName = data.buy.buyName
                this.dataForm.buyStarttime = data.buy.buyStarttime
                this.dataForm.buyExprietime = data.buy.buyExprietime
                this.dataForm.buyCount = data.buy.buyCount
                this.dataForm.buyPrice = data.buy.buyPrice
                this.dataForm.buyNum = data.buy.buyNum
                this.dataForm.buyAging = data.buy.buyAging
                this.dataForm.buyAmount = data.buy.buyAmount
                this.dataForm.buyFreight = data.buy.buyFreight
                this.dataForm.buyMs = data.buy.buyMs
                this.dataForm.buyUrl = data.buy.buyUrl
                this.dataForm.shopId = data.buy.shopId
                this.dataForm.shopUrl = data.buy.shopUrl
                this.dataForm.shopName = data.buy.shopName
                this.dataForm.shopPrice = data.buy.shopPrice
                this.dataForm.shopRepertory = data.buy.shopRepertory
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/commerce/buy/${!this.dataForm.buyId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'buyId': this.dataForm.buyId || undefined,
                'buyName': this.dataForm.buyName,
                'buyStarttime': this.dataForm.buyStarttime,
                'buyExprietime': this.dataForm.buyExprietime,
                'buyCount': this.dataForm.buyCount,
                'buyPrice': this.dataForm.buyPrice,
                'buyNum': this.dataForm.buyNum,
                'buyAging': this.dataForm.buyAging,
                'buyAmount': this.dataForm.buyAmount,
                'buyFreight': this.dataForm.buyFreight,
                'buyMs': this.dataForm.buyMs,
                'buyUrl': this.dataForm.buyUrl,
                'shopId': this.dataForm.shopId,
                'shopUrl': this.dataForm.shopUrl,
                'shopName': this.dataForm.shopName,
                'shopPrice': this.dataForm.shopPrice,
                'shopRepertory': this.dataForm.shopRepertory
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
